#include "CNetwork.h"

double radiusCentre=200.0;
double radiusWifi = 100.0;
//---------------------------------------------------------------------------
// Ajout d'un centre
void CNetwork::addCentre(std::string p_str_id, bool remove)
{
	if(!remove)
	{
		for(int i=0; i<vnodes.size(); i++)
		{
			//printf("%s, %s",p_str_id.c_str(), vnodes[i]->getName());
			if(p_str_id.compare(vnodes[i]->getName())==0)
			{
				centres.push_back(vnodes[i]);
			}
		}
	}
	else
	{
		for(int i=0; i<centres.size(); i++)
		{
			if(p_str_id.compare(centres[i]->getName())==0)
			{
				std::vector<CNode *>::iterator it = centres.begin() + i;
				centres.erase(it);
			}
		}
	}
}


//---------------------------------------------------------------------------
// Rcupre tous les noeuds en x, y
void CNetwork::getNodesAt(double p_d_x, double p_d_y, std::vector <CNode *> *p_pN_nodesAt)
{
	unsigned int i;

	p_pN_nodesAt->clear();
	for(i = 0; i < vnodes.size(); i++)
	{
		if(sqrt( pow(p_d_x-vnodes[i]->getX(),2) + pow(p_d_y-vnodes[i]->getY(),2)) < vnodes[i]->getRadius())
		{
			p_pN_nodesAt->push_back(vnodes[i]);
		}
	}
}


//---------------------------------------------------------------------------
// Rcupre le premier noeud en x, y
CNode *CNetwork::getFirstNodeAt(double p_d_x, double p_d_y)
{
	std::vector <CNode *> *pN_nodesAt = new std::vector<CNode *>();
	CNode *pN_nodeAt = NULL;

	getNodesAt(p_d_x, p_d_y, pN_nodesAt);
	if(pN_nodesAt->size()>0)
	{
		pN_nodeAt = (*pN_nodesAt)[0];
	}
	delete pN_nodesAt;
	return pN_nodeAt;
}


//---------------------------------------------------------------------------
// Sauvegarde le rseau en SVG
void CNetwork::insertNode(CNode *p_N_node)
{
	// Portee Wifi
	out << "<circle cx='" << p_N_node->getX() << "' cy='" << p_N_node->getY() <<"' r='" << radiusWifi <<"' stroke='#CCC' stroke-width='1' fill='none' stroke-dasharray='4,2'/>" << std::endl;

	// Noeud
	out << "<circle cx='" << p_N_node->getX() << "' cy='" << p_N_node->getY() << "' r='" << p_N_node->getRadius() << "' stroke='black' stroke-width='1' fill='#DFD'/>" << std::endl;

	// Identifiant
	out << "<text x='" << p_N_node->getX()-4 << "' y='" << p_N_node->getY()+4 << "' font-family='Verdana' font-size='10' fill='black'>" << p_N_node->getName() << "</text>" << std::endl;
}


void CNetwork::insertArc(double x, double y, double x1, double y1)
{
	out << "<line x1='" << x << "' y1='" << y << "' x2='" << x1 << "' y2='" << y1 << "' stroke-width='1'/>" << std::endl;
	//stroke-dasharray='4,2'
}


void CNetwork::filesav(std::string filename)
{
	out.open(filename.c_str());

	// Entete SVG
	out << "<?xml version='1.0' encoding='utf-8'?><svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:ev='http://www.w3.org/2001/xml-events' version='1.1' baseProfile='full' x='0' y='0'  width='800' height='800' id='svg2'><title>Network</title>" << std::endl;

	// Portee des centres
	for(int i=0; i<centres.size();i++)
	{
		out << "<ellipse cx='" << centres[i]->getX() << "' cy='" << centres[i]->getY() << "' rx='" << radiusCentre << "' ry='" << radiusCentre << "' stroke='black' stroke-width='0' fill='#EEE'/>" << std::endl;
	}

	// Ajout des arcs
	out << "<g stroke='green'>" << std::endl;
	for(int i=0; i<nbNodes(); i++)
	{
		for(int j=i; j<nbNodes(); j++)
		{
			if(i!=j && sqrt(pow((double)(getNode(i)->getX()-getNode(j)->getX()),2.0)+pow((double)(getNode(i)->getY()-getNode(j)->getY()),2.0))<radiusWifi)
			{
				insertArc(getNode(i)->getX(), getNode(i)->getY(), getNode(j)->getX(), getNode(j)->getY());
			}
		}
	}
	out << "</g>" << std::endl;

	// Ajout des noeuds
	for(int i=0; i<nbNodes(); i++)
	{
		insertNode(getNode(i));
	}
	out << "</svg>" << std::endl;
	out.close();
}
